################################################################################
# script to extract data at dates and times of interest

################################################################################
# set the constants

#define some directories
work.dir = '/homes/31/jc165798/working/April/weather.climate/'
data.dir = '/homes/31/jc165798/Climate/AWAP_CSIRO/baseline_Austalia_5km/monthly.summaries/csv/'

#data file
data.file = 'all_birds_unique_locations.csv'

#define the months in character
months.char = c('01','02','03','04','05','06','07','08','09','10','11','12')

################################################################################
#set the working directory
setwd(work.dir)

#read in the data file
indata = read.csv(data.file,as.is=T)
#reorder by year, month, lat, lon
indata=indata[order(indata$YEAR,indata$MONTH,indata$LATDEC,indata$LONGDEC),]

#get the unique combinations of year & month
month.year = unique(indata[1:2])
nrow(month.year)

#define the output data
outdata = indata

#cycle through the unique month year combinations
for (i in 1:nrow(month.year)){
  print(month.year[i,])
  #read in the data
  foleys = read.csv(paste(data.dir,month.year$YEAR,months.char[month.year$MONTH],'.foleys.csv',sep=''),as.is=T)
  prec = read.csv(paste(data.dir,month.year$YEAR,months.char[month.year$MONTH],'.precip.csv',sep=''),as.is=T)
  temp = read.csv(paste(data.dir,month.year$YEAR,months.char[month.year$MONTH],'.temper.csv',sep=''),as.is=T)
  #define the rows of indata which are associated with this month.year
  trows = which(indata$MONTH==month.year$MONTH[i] & indata$YEAR==month.year$YEAR[i])
  #setup outdata if still empty
  if (length(outdata)==length(indata)){
    for (tname in names(foleys)) {if(tname!='lat' & tname!='lon') {outdata[[tname]]=NA}}
    for (tname in names(prec)) {if(tname!='lat' & tname!='lon') {outdata[[tname]]=NA}}
    for (tname in names(temp)) {if(tname!='lat' & tname!='lon') {outdata[[tname]]=NA}}
  }
  #get tvals ... the rows of the data files that correspond to the lat/longs of the trows
  tvals = NULL
  for (trow in trows) {
    ttc = which(prec$lat==indata$LATDEC[trow] & prec$lon==indata$LONGDEC[trow])
    if (length(ttc)>0) {tvals = c(tvals,ttc)} else{tvals = c(tvals,NA)}
  }
  #write out the data from trows
  outdata[trows,5:12] = foleys[tvals,-c(1:2)]
  outdata[trows,13:35] = prec[tvals,-c(1:2)]
  outdata[trows,36:58] = temp[tvals,-c(1:2)]
}

write.csv(outdata,'background.4.weather.work.csv',row.names=F,na='')
